﻿/// /////////////////////////////////////////////
// 单元名 : frameDLItem
// 作者 : Hawk
// 单元描述 :
// 日期 : 2023-09-15
/// /////////////////////////////////////////////

{$IFDEF FPC}
{$H+}
{$MODE DELPHI}
{$ENDIF}
unit frameDLItem;

interface

uses
{$IFNDEF FPC}
  Windows,
{$ELSE}
  LCLIntf, LCLType, LMessages,
{$ENDIF}
  Messages, SysUtils, Classes, Graphics, Controls, Forms, StdCtrls, ExtCtrls, unitFileAndTransCommon, ComCtrls,
  Buttons, unitCommonDataStruct;

type
  TfrDownloadItem = class(TFrame)
    pMain: TPanel;
    lDLCaption: TLabel;
    lCurStatus: TLabel;
    lAllSize: TLabel;
    lLeftTime: TLabel;
    pbAllProgress: TProgressBar;
    bDelDL: TBitBtn;
    bPause: TBitBtn;
  private
    { Private declarations }
    FDiskSpeed: Longword;
    FCompLen: int64;
    FDLID: TID_OBJ;
    FElapseTick: Longword;
    FNetSpeed: Longword;
    FAllLen: int64;
    FStatus: TDownloadStatus;
    procedure ToUI;
  public
    { Public declarations }
    procedure SetDLItem(const Value: TDownloadItem);

    property DLID: TID_OBJ read FDLID;
    property Status: TDownloadStatus read FStatus;
    property AllLen: int64 read FAllLen;
    property CompLen: int64 read FCompLen;
    property ElapseTick: Longword read FElapseTick;
    property NetSpeed: Longword read FNetSpeed;
    property DiskSpeed: Longword read FDiskSpeed;
  end;

const
  I_TRANSFRAMEWIDTH = 400;
  I_TRANSFRAMEHEIGHT = 100;

implementation

uses unitObjPool, unitMisc;

{$IFNDEF FPC}
{$R *.dfm}
{$ELSE}
{$R *.lfm}
{$ENDIF}
{ TfrDownloadItem }

procedure TfrDownloadItem.SetDLItem(const Value: TDownloadItem);
begin
  if Value <> nil then
  begin
    lDLCaption.Caption := Value.Caption;
    FDLID := Value.ObjID;
    FDiskSpeed := Value.DiskSpeed;
    FCompLen := Value.CompLen.fp64;
    FElapseTick := Value.ElapsTime;
    FNetSpeed := Value.NetSpeed;
    FAllLen := Value.DownloadLen.fp64;
    FStatus := Value.Status;
    ToUI;
  end;
end;

procedure TfrDownloadItem.ToUI;
var
  s: string;
  spd: Longword;
begin
  s := S_DOWNLOADSTATUS[FStatus];
  lCurStatus.Caption := s;
  lAllSize.Caption := Format('总传输 %s， 已传输 %s', [SizeToStr(FAllLen), SizeToStr(FCompLen)]);
  s := Format('历时 %s', [SecondToDateStr(FElapseTick)]);
  if FNetSpeed <> 0 then
    s := s + Format('，网络 %s/秒', [SizeToStr(FNetSpeed)]);
  if FDiskSpeed <> 0 then
    s := s + Format('，存储 %s/秒', [SizeToStr(FDiskSpeed)]);
  if FNetSpeed > FDiskSpeed then
    spd := FDiskSpeed
  else
    spd := FNetSpeed;
  if spd <> 0 then
    s := s + Format('，还需 %s', [SecondToDateStr((AllLen - CompLen) div spd)]);
  lLeftTime.Caption := s;
  if AllLen <> 0 then
    pbAllProgress.Position := FCompLen * 100 div FAllLen
  else
    pbAllProgress.Position := 0;
end;

end.
